package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.course.CourseStudyHistoryService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.CourseType;
import com.qdxwx.models.user.User;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/*
 * course课程api
 * */
@Controller
@RequestMapping("/ajax/course")
public class AjaxCourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseNodeService courseNodeService;
    @Autowired
    private CourseStudyHistoryService courseStudyHistoryService;

    @GetMapping("/get.json")
    @ResponseBody
    public AjaxResult view(HttpServletRequest request){
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if(courseId < 1) return AjaxResult.error("参数异常");
        Course course = courseService.get(courseId);
        if(course == null) return AjaxResult.error("找不到指定课程");
        return AjaxResult.success(course);
    }

    @GetMapping("/list.json")
    @ResponseBody
    public AjaxResult bannerList(HttpServletRequest request){
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        Integer categoryId = TKConvert.toInteger(request.getParameter("categoryId"),null);
        Integer subjectId = TKConvert.toInteger(request.getParameter("subjectId"),null);
        Integer elite = TKConvert.toInteger(request.getParameter("elite"),null);
        Integer isTop = TKConvert.toInteger(request.getParameter("isTop"),null);
        Integer teacherId = TKConvert.toInteger(request.getParameter("teacherId"),null);
        Integer memberTypeId = TKConvert.toInteger(request.getParameter("memberTypeId"),null);
        Integer isFree = TKConvert.toInteger(request.getParameter("isFree"));
        Integer orderBy = TKConvert.toInteger(request.getParameter("orderBy"));
        String sellType = CourseType.getString(request.getParameter("sellType"));
        int page = TKConvert.toInteger(request.getParameter("page"),1);
        int limit = TKConvert.toInteger(request.getParameter("limit"),5);
        CourseQuery query = new CourseQuery();
        query.setChannelId(channelId);
        query.setIsTop(isTop);
        query.setSubjectId(subjectId);
        query.setCategoryId(categoryId);
        query.setIsElite(elite);
        query.setTeacherId(teacherId);
        query.setMemberTypeId(memberTypeId);
        query.setIsFree(isFree);
        query.setSellType(sellType);
        query.setIsAvailable(1);
        query.setOrderBy(orderBy);
        PageRequest pageRequest = new PageRequest();
        pageRequest.setCurrentPage(page);
        pageRequest.setPageSize(limit);
        List<Course> courseList = courseService.getList(query, pageRequest);
        return AjaxResult.success(courseList);
    }

    @RequestMapping("/my/list.json")
    @ResponseBody
    public AjaxResult myList(HttpServletRequest request){
        User user = LoginUser.getUser(request);
        if(user == null) return AjaxResult.error("登录超时");
        int page = TKConvert.toInteger(request.getParameter("page"),1);
        int limit = TKConvert.toInteger(request.getParameter("limit"),6);
        String sellType = CourseType.getString(request.getParameter("sellType"));
        CourseQuery query = new CourseQuery();
        query.setUserId(user.getUserId());
        query.setSellType(sellType);
        PageRequest pageRequest = new PageRequest();
        pageRequest.setCurrentPage(page);
        pageRequest.setPageSize(limit);
        List<CourseDTO> courseList = courseService.getMyCourseList(query, pageRequest);

        courseStudyHistoryService.setStudyPercent(courseList, user.getUserId());

        return AjaxResult.success(courseList);
    }
}
